package uk.co.chrisdadswell.radioreminder;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class activityDashboard extends Activity
        implements fragmentStations.OnStationSelectedListener,
        fragmentBroadcasts.OnBroadcastSelectedListener {

    static final String APP_TAG = "rr: ";
    static final String ACT_TAG = "activityDashboard: ";
    static final String KEY_TIME = "broadcastDurations";
    static final String KEY_TITLE = "broadcastTitles";
    static final String KEY_SUBTITLE = "broadcastSubtitles";
    static final String KEY_SYNOPSIS = "broadcastSynopsis";
    static final String KEY_EPISODE_URL = "broadcastEpisodeUrl";


    @Override
    public void onBackPressed() {
        finish();
        //overridePendingTransition(android.R.anim.fadeout, android.R.anim.push_down);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(APP_TAG, ACT_TAG + "... OnStart ....");
        Log.d(APP_TAG, ACT_TAG + "Initialize directories");
        InitDirectories();
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(APP_TAG, ACT_TAG + "... OnPause ...");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(APP_TAG, ACT_TAG + "... OnDestroy ...");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        if (findViewById(R.id.fragment_content) != null) {
            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            fragmentStations stationsFragment = new fragmentStations();
            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            stationsFragment.setArguments(getIntent().getExtras());
            // Add the fragment to the 'fragment_content' FrameLayout
            getFragmentManager().beginTransaction().add(R.id.fragment_content, stationsFragment).commit();
        }
    }

    public void onStationSelected(String stationSelected, String stationScheduleUrl) {
        // The user selected a station from the fragmentStations
/*
        // Capture the article fragment from the activity layout if it is two pane
        fragmentStations stationsFrag = (fragmentStations)
                getFragmentManager().findFragmentById(R.id.article_fragment);
        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...
            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);
        } else {
*/
            // If the frag is not available, we're in the one-pane layout and must swap frags...
            // Create fragment and give it an argument for the selected station
            fragmentBroadcasts newFragment = new fragmentBroadcasts();
            Bundle args = new Bundle();
            args.putString(constants.STATION_NAME, stationSelected);
            args.putString(constants.STATION_SCHEDULE_URL, stationScheduleUrl);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.replace(R.id.fragment_content, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
    }

    @Override
    public void onBroadcastSelected(String broadcastSelected) {
        // The user selected a broadcast from the fragmentBroadcasts
    }

    // CREATE MENUS
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.stations_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // MENU SELECTORS
    @Override
    public boolean onOptionsItemSelected (MenuItem item){
        switch (item.getItemId()){

            case android.R.id.home:
                Intent intent = new Intent(this, activityDashboard.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;

            /*case R.id.menu_about:
                Intent aboutActivity = new Intent(Activity_Dashboard.this, Activity_About.class);
                startActivity(aboutActivity);
                return true;

            case R.id.menu_refresh:
                downloadNewListingsDialog(this.getResources().getString(R.string.download_listings_dialog_title), this.getResources().getString(R.string.download_listings_dialog_body));
                refresh.setActionView(R.layout.actionbar_indeterminate_progress);
                return true;

            case R.id.menu_blog:
                Intent blogURL_intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(Constants.urlBlog));
                startActivity(blogURL_intent);
                return true;

            case R.id.menu_gplus:
                Intent gplusURL_intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(Constants.urlGooglePlus));
                startActivity(gplusURL_intent);
                return true;

            case R.id.menu_rss:
                Intent rssURL_intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(Constants.urlRss));
                startActivity(rssURL_intent);
                return true;

            case R.id.menu_twitter:
                Intent twitter_intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(Constants.urlTwitter));
                startActivity(twitter_intent);
                return true;*/
        }
        return false;
    }

    private void InitDirectories() {
        functions_io.CreateADirectory(constants.rootDir);
        functions_io.CreateADirectory(constants.stationsDir);
        functions_io.CreateADirectory(constants.broadcastsDir);
        functions_io.CreateADirectory(constants.schedulesDir);
        functions_io.CreateADirectory(constants.broadcastStationsDir);
        functions_io.CreateADirectory(constants.schedulesStationsDir);
    }

}